﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using OutlookHub.Styles;
using MAPIdotnet;
using Silvermoon.Images;
using Silvermoon.Windows;
using System.Text.RegularExpressions;
using Silvermoon.OpenGL;
using OutlookHub.Utils;
using Silvermoon.Controls.Forms;
using Silvermoon.UI;
using OutlookHub.Models;
using Silvermoon.Shapes;
using System.Threading;
using System.Diagnostics;
using Silvermoon.Animations;

namespace OutlookHub.Pages
{
    public class EMailDetailsPage : Page
    {
        private string contentText;
        private TextBox content;
        private Label titleLabel, fromLabel;
        private Message msg;
        private static VisualStateManager textboxManager = new VisualStateManager();
        private ProgressingControl progressing;

        public EMailDetailsPage(Message msg)
            : base()
        {
            this.msg = msg;
            InitializePage();
        }

        private void InitializePage()
        {
            //BackTransition = Silvermoon.Navigation.PageTransitionMode.Hide;
            Styles = StylePool.EmailDetails;
            Title = "EMAIL DETAILS";
            //SetTransition(350, TransitionMask.Zoom | TransitionMask.Fade, 500, TransitionMask.Explode | TransitionMask.Fade);
            SetTransition(350, TransitionMask.RotateLeftIn, TransitionMask.RotateLeftIn);

            int r = ScrollViewer.ScrollbarWidth + 2;
            //Label toLabel = new Label { AutoSize = true, LineMode = LineMode.SingleLine, Text = "to: " + msg.Recipients[0].Name, Opacity = 240 };
            Label dateLabel = new Label { AutoSize = true, LineMode = LineMode.SingleLine, Text = msg.DeliveryDate.ToLongDateString(), Opacity = 220, Margin = new Margin(5, 0, 5, 0), FontScale = 0.9f };
            fromLabel = new Label
            {
                AutoSize = true,
                Margin = new Margin(5, 5, r, 5),
                Font = FontPool.BigRegularFont,
                LineMode = LineMode.WordWrap,
                FontScale = 1.2f,
                Text = msg.Sender
            };
            titleLabel = new Label
            {
                AutoSize = true,
                Margin = new Margin(5, 5, r, 10),
                LineMode = LineMode.WordWrap,
                Text = msg.Subject,
                TextColor = PhoneColors.Accent,
                Font = FontPool.MediumRegularFont
            };

            content = new TextBox
            {
                AcceptReturn = true,
                Margin = new Margin(5, 15, r, 0),
                AutoHeight = true,
                ReadOnly = true,
                StateManager = textboxManager,
                Chrome = Shape.Empty,
                Color = PhoneColors.White
            };

            ScrollStackPanel panel = new ScrollStackPanel { Margin = new Margin(0, 0, 0, 8) };
            panel.Controls.Add(fromLabel, titleLabel, dateLabel, content);
            Controls.Add(panel);

            progressing = new ProgressingControl { Margin = new Margin(0, null, 0, 0), Visible = true, TextColor = PhoneColors.Black, Height = 8 };
            Controls.Add(progressing);

            Toolbar tb = new Toolbar();
            //            tb.TextColor = PhoneColors.Black;
            tb.AddButton(SysImages.Back, Page.BackCommand);
            tb.AddButton(SysImages.RecycleBin).Enabled = false;
            Toolbar = tb;
            GetBody();

        }


        protected override void OnShown()
        {
            base.OnShown();
            if (!string.IsNullOrEmpty(contentText)) SetText(contentText);
        }

        private void GetBody()
        {
            try
            {
                int t0 = Environment.TickCount;
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    Thread.CurrentThread.Priority = ThreadPriority.Lowest;
                    Thread.Sleep(0);
                    string s = msg.Body.HtmlToAscii();
                    Thread.Sleep(100);
                    Invoke(() => SetText(s));
                });
                int t1 = Environment.TickCount - t0;
                Trace.WriteLine("Delay = " + t1.ToString());
            }
            catch (Exception ex)
            {
                MessageDialog.Show("ERROR", ex.Message, GoBack, GoBack);
            }
        }

        private void SetText(string s)
        {
            contentText = null;
            if (Visibility == 1f)
            {
                content.Text = s;
                IntTransition transition = new IntTransition { From = 0, To = 255, Duration = 400, EasingFunction = LinearEase.Instance };
                transition.Completed += new EventHandler(transition_Completed);
                OpacityProperty.Animate(content, transition);
            }
            else contentText = s;
        }

        void transition_Completed(object sender, EventArgs e)
        {
            progressing.Visible = false;
        }

        protected override void OnHome(HandledEventArgs e)
        {
            Screen.Renderer.BackgroundColor = PhoneColors.Black;
            base.OnHome(e);
        }
    }
}
